<template name="stockSale">
<view class="bgzd">
	<view class="form">
		<view class="form_top">
			<text>商品购买</text>
			<image @click="close" src="/static/img/none_close.png"></image>
		</view>
		
		<view class="view_input">
		    <view class="view_input_title">物品名称:</view>
		    <input class="view_input_input" type="text" v-model="item.name"
		    placeholder-class="placeholderClass" disabled>
		</view>
		<view class="view_input">
		    <view class="view_input_title">物品单价:</view>
		    <input class="view_input_input" type="text" v-model="item.price"
		    placeholder-class="placeholderClass" disabled>
		</view>
		<view class="view_input">
		    <view class="view_input_title">购买数量:</view>
		    <input class="view_input_input" type="number" v-model="form.num" 
			@input="give"
		    placeholder-class="placeholderClass" placeholder="请输入购买数量">
		</view>
		<view class="view_input">
		    <view class="view_input_title">客户姓名:</view>
		    <picker @change="bindNameIndex" :value="NameIndex" :range="list"
		    range-key="username" v-if="list.length > 0" class="view_input_input">
				<view class="uni-input">
				{{list[NameIndex]['username']}}
				<image class="picker_img" src="/static/img/buttom.png"></image>
				</view>
		    </picker>
		</view>
		
		<view class="view_input">
		    <view class="view_input_title">应收金额:</view>
		    <input class="view_input_input" type="number" :value="form.num*item.price"
		    placeholder-class="placeholderClass" disabled>
		</view>
		<view class="view_input">
		    <view class="view_input_title">代金券:</view>
			<input class="view_input_input" type="text" v-model="form.give_money" 
			@input="give"
			placeholder-class="placeholderClass" placeholder="请输入代金券">
		</view>
		<view class="view_input">
		    <view class="view_input_title">账户抵扣:</view>
		    <input class="view_input_input" type="number" v-model="form.offset_money" 
			@input="give"
		    placeholder-class="placeholderClass" placeholder="请输入账户抵扣">
		</view>
		<view class="view_input">
		    <view class="view_input_title">实收金额:</view>
		    <input class="view_input_input" type="number" v-model="form.pay_price"
		    placeholder-class="placeholderClass" disabled>
		</view>
		
		<view class="form_footer">
			<button v-if="addTrue" @click="addStock" class="strue">添加</button>
			<button v-else class="sfalse">添加</button>
		</view>
	</view>
</view>
</template>
<script>
import request from '@/util/request.js'
export default {
	name: "stockSale",
	//属性
	props:['item'],
	data() {
		return {
			imgUrl:this.url,
			list:[],
			NameIndex:0,
			addTrue:true,
			form:{
				num:0,
				pay_money:'',
				give_money:'',
				offset_money:''
			}
		}
	},
	//组件生命周期
	created:function(){
		this.getList()
	},
	methods:{
		give:function(){
			if(this.form.offset_money){
				var offset = this.form.offset_money
			}else{
				var offset = 0
			}
			if(this.form.give_money){
				var give = this.form.give_money
			}else{
				var give = 0
			}
			this.form.pay_price = this.item.price*this.form.num - give - offset
		},
		close:function(){
			this.$emit('func',{'type':1,'model':'stockSaleShow'})
		},
		getList:function(){
			var data = {
			    oa_id:uni.getStorageSync('oa_id')
			}
			var that = this
			request({
				url: 'manage/member/all',
				method: 'get',
				data:data,
				success: (res) => {
					that.list = res.data.data
					that.form.member_id = res.data.data[0]['id']
				},
			})
		},
		addStock:function(){
			if(!this.form.num){
				uni.showToast({
					title: '请填写购买数量!!!',
					icon: "none", 
					position:'bottom',
				});
				return;
			}
			this.form.price = this.form.num*this.item.price
			this.form.oa_id = uni.getStorageSync('oa_id')
			this.form.goods_id = this.item.id
			this.addTrue = false
			var that = this
			request({
				url: 'manage/goods/addOrder',
				method: 'post',
				data:this.form,
				success: (res) => {
					if(res.data.code == 1){
						uni.showToast({
							title: '购买成功!!!',
							icon: "none", 
							position:'bottom',
						});
						that.$emit('func',{'type':1,'model':'stockSaleShow'})
					}else{
						uni.showToast({
							title:res.data.msg,
							icon: "none", 
							position:'bottom',
						});
					}
				},
			})
		},
		bindNameIndex:function(e){
			this.NameIndex = e.detail.value
			this.form.member_id = this.list[e.detail.value]['id']
		}
	},
}
</script>
<style>
.bgzd{
	position: fixed;
	top: 0;
	left: 0;
	bottom: 0;
	right: 0;
	width: 100%;
	height: 100%;
	background: rgba(0,0,0,0.4);
	z-index:999;
}
.form{
    padding-top:20rpx;
    padding-left:2%;
    width:100%;
    height:70%;
    bottom:0;
    padding-bottom: 20rpx;
    background:#d9d9d9;
    position: fixed;
    z-index:9999;
}
.form_top{
    font-weight:bold;
    font-size:20px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    width:95%;
}
.form_top image{
    width:35rpx;height:35rpx;
    margin-right: 20rpx;
}
.form_footer{
    width:95%;
    margin-top:60rpx;
}
.picker_img{
	width:35rpx;height:35rpx;
}
.strue{
	width:100%;
	height:80rpx;
	line-height:80rpx;
	background:#ff861a;
	color:#fff;
}
.sfalse{
	width:100%;
	height:80rpx;
	line-height:80rpx;
	background:#8c8c8c;
	color:#fff;}
.frame_img{
	width:250rpx;height:200rpx;
	border:1rpx dashed #000000;
}
.radioClass{
	display:flex;
	flex-direction: row;
	justify-content:flex-start;
}
.row{
	display:flex;
	flex-direction: row;
	justify-content:flex-start;
}
</style>